package com.example.text2.moudle;

import com.example.text2.http.Api;
import com.example.text2.http.MyServer;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.Url;

/**
 * Created by 段傅华 on 2018/12/13.
 */

public class Moudle implements Main_Moudle{
    public MyServer r(String ulr){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ulr)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        MyServer myServer = retrofit.create(MyServer.class);
        return myServer;
    }
    @Override
    public void setData(int data, final Api api, final Finish finish) {
        switch (api){
            case VIDEO:
                MyServer r = r(MyServer.mUrl);
                Observable<String> getcount = r.getcount(data,"sd");
                getcount.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String value) {
                                    finish.setSuccess(value,api);
                            }

                            @Override
                            public void onError(Throwable e) {
                                    finish.setError(e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
            case BOON:
                MyServer r1 = r(MyServer.mUrl2);
                Observable<String> stringObservable = r1.getcount2(data);
                stringObservable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String value) {
                                finish.setSuccess(value,api);
                            }

                            @Override
                            public void onError(Throwable e) {
                                finish.setError(e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
        }
    }
}
